package com.nl.controller.sms5g.servlet;

import com.nl.controller.sms5g.bean.AttachInfo;
import com.nl.controller.web.global.GlobalConst;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.IOUtils;
import sun.nio.ch.IOUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@WebServlet(name="imageShowServlet",urlPatterns="/servlet/h5imageShowServlet")
@Slf4j
public class ImageShowServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String fileName = request.getParameter("fileName");
        String type = request.getParameter("type");
        System.out.println("---"+fileName);
        ServletOutputStream out = null;
        FileInputStream ips = null;
        String path="";
        String filePath="";
        AttachInfo info = new AttachInfo();
        if("1".equals(type)){
            info.setFilePath(GlobalConst.SIGNNATURE_FILE_PATH);
            log.info("===find sign===");

        }else if("2".equals(type)){
            info.setFilePath(GlobalConst.MULTI_FILE_PATH);
            log.info("===find multi===");
        }

        info.setFileName(fileName);
        if(info!=null){
            fileName=info.getFileName()==null?"":info.getFileName();
            filePath=info.getFilePath()==null?"":info.getFilePath();
            path=filePath+fileName;
            log.info("====="+path+"=====");
            try {
                File file =new File(path);
                if(file.exists()&& file.isFile()){
                    log.info("=====file exists=====");
                    //获取图片存放路径
                    ips = new FileInputStream(file);
//	        			response.setContentType("image/*")
                    response.setContentType("application/octet-stream");
                    //9.设置响应头控制浏览器不要缓存
                    response.setDateHeader("expries", -1);
                    response.setHeader("Cache-Control", "no-cache");
                    response.setHeader("Pragma", "no-cache");
                    response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
                    response.addHeader("Content-Length", "" + file.length());
                    out = response.getOutputStream();
                    //读取文件流
                    int len = 0;
                    byte[] buffer = new byte[1024 * 10];
                    while ((len = ips.read(buffer)) != -1){
                        out.write(buffer,0,len);
                    }
                    out.flush();
                }


            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(out!=null){
                    out.close();
                }
                if(ips!=null){
                    ips.close();
                }
            }
        }else{
            out = response.getOutputStream();
            response.setContentType("image/*");
            //9.设置响应头控制浏览器不要缓存
            response.setDateHeader("expries", -1);
            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Pragma", "no-cache");
            out.write("error".getBytes());
            out.flush();
            if(out!=null){
                out.close();
            }
        }
    }
}
